package zzxkj.blog.search.mq.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import zzxkj.blog.config.RabbitConfig;
import zzxkj.blog.search.mq.entity.EsMessage;
import zzxkj.blog.service.SearchService;

/**
 * es修改消息消费者
 * @author zzxkj
 */
@Slf4j
@Component
@RabbitListener(queues = RabbitConfig.es_queue)
public class EsMessageHandler {

    final
    SearchService searchService;

    public EsMessageHandler(SearchService searchService) {
        this.searchService = searchService;
    }

    @RabbitHandler
    public void handler(EsMessage message) {

        log.info("mq 消费一条es相关消息： {}", message.toString());

        switch (message.getType()) {
            case EsMessage.CREATE_OR_UPDATE:
                searchService.createOrUpdateIndex(message);
                break;
            case EsMessage.REMOVE:
                searchService.removeIndex(message);
                break;
            default:
                log.error("没找到对应的消息类型，请注意！！ --》 message:{}", message.toString());
                break;
        }
    }

}
